<template>
  <div class="editInfo">
    <div class="title">
      <van-nav-bar :title="title">
        <template #left>
          <i
            class="iconfont iconbtn_nav_back iconStyle"
            @click="$router.go(-1)"
          ></i>
        </template>
        <template #right>
          <span v-if="saveEdit" @click="saveChange">保存</span>
        </template>
      </van-nav-bar>
    </div>
    <div class="content">
      <van-field
        v-model="message"
        rows="1"
        autosize
        type="textarea"
        class="txtstyle"
        @input="saveEdit = true"
      />
    </div>
  </div>
</template>

<script>
import { getEdit, getuserInfo } from '@/api/login'

export default {
  data () {
    return {
      title: '',
      saveEdit: false,
      message: ''
    }
  },
  created () {
    // console.log(this.$route.query.prototype)
    if (this.$route.query.prototype === 'nickname') {
      this.title = '修改昵称'
      // this.message = this.$store.state.userInfo.nickname // 简化
    } else if (this.$route.query.prototype === 'intro') {
      this.title = '修改个性签名'
      // this.message = this.$store.state.userInfo.intro  // 简化
    }
    this.message = this.$store.state.userInfo[this.$route.query.prototype]
  },
  methods: {
    async saveChange () {
      this.$toast.loading({
        message: '加载中...',
        duration: 0
      })
      // 简化
      // if (this.$route.query.prototype === 'nickname') {
      //   await getEdit({ nickname: this.message })
      //   getuserInfo().then(res => {
      //     res.data.data.avatar = 'http://127.0.0.1:1337' + res.data.data.avatar
      //     this.$store.commit('setUserInfo', res.data.data)
      //   })
      // } else if (this.$route.query.prototype === 'intro') {
      //   await getEdit({ intro: this.message })
      //   getuserInfo().then(res => {
      //     res.data.data.avatar = 'http://127.0.0.1:1337' + res.data.data.avatar
      //     this.$store.commit('setUserInfo', res.data.data)
      //   })
      // }

      await getEdit({ [this.$route.query.prototype]: this.message })
      getuserInfo().then(res => {
        res.data.data.avatar = 'http://127.0.0.1:1337' + res.data.data.avatar
        this.$store.commit('setUserInfo', res.data.data)
      })
      this.$toast.success('修改成功')
      this.$router.go(-1)
    }
  }
}
</script>

<style lang="less" scoped>
.editInfo {
  font-size: 12px;
  .title {
    .iconStyle {
      font-size: 44px;
    }
    /deep/.van-nav-bar__left {
      padding-left: 0;
    }
    /deep/.van-nav-bar__title {
      font-weight: 700;
    }
  }
  .content {
    padding: 15px;
  }
  .txtstyle {
    background-color: #f5f3f4;
  }
}
</style>
